{{- define "upmeter_resources" }}
cpu: 50m
memory: 50Mi
{{- end }}

{{- if (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: upmeter
  namespace: d8-{{ .Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "upmeter")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: StatefulSet
    name: upmeter
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: upmeter
      minAllowed:
        {{- include "upmeter_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 100m
        memory: 100Mi
    {{- include "helm_lib_vpa_kube_rbac_proxy_resources" . | nindent 4 }}
{{- end }}
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: upmeter
  namespace: d8-upmeter
  {{- include "helm_lib_module_labels" (list . (dict "app" "upmeter")) | nindent 2 }}
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      app: upmeter
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: upmeter
  namespace: d8-{{ .Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "upmeter")) | nindent 2 }}
spec:
  replicas: 1
  serviceName: upmeter
  selector:
    matchLabels:
      app: upmeter
  template:
    metadata:
      labels:
        app: upmeter
    spec:
      imagePullSecrets:
      - name: deckhouse-registry
      terminationGracePeriodSeconds: 5
      enableServiceLinks: false
      serviceAccountName: upmeter
      {{- include "helm_lib_node_selector" (tuple . "monitoring") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple . "monitoring" "without-storage-problems") | nindent 6 }}
      {{- include "helm_lib_priority_class" (tuple . "cluster-low") | nindent 6 }}
      {{ include "helm_lib_module_pod_security_context_run_as_user_deckhouse" . | nindent 6 }}
      initContainers:
{{- $storageClass := .Values.upmeter.internal.effectiveStorageClass }}
{{- if $storageClass }}
      {{- include "helm_lib_module_init_container_chown_deckhouse_volume" (tuple . "data") | nindent 6 }}
{{- end }}
      - name: migrator
        image: {{ include "helm_lib_module_image" (list . "upmeter") }}
        command:
          - /migrate
          - -verbose
          - "-path=/data/migrations/server"
          - "-database=sqlite3:///db/downtime.db.sqlite?x-no-tx-wrap=true"
          - up
        volumeMounts:
          - mountPath: /db
            name: data
            readOnly: false
          - mountPath: /tmp
            name: tmp
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" . | nindent 8 }}
        resources:
          requests:
{{- if $storageClass }}
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
{{- else }}
            {{- include "helm_lib_module_ephemeral_storage_logs_with_extra" 1024 | nindent 12 }}
{{- end }}
{{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "upmeter_resources" . | nindent 12 }}
{{- end }}
      containers:
      - name: upmeter
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" . | nindent 8 }}
        image: {{ include "helm_lib_module_image" (list . "upmeter") }}
        command:
          - /upmeter
          - start
          - --origins={{ index .Values.global.discovery "clusterMasterCount" }}
          - --user-agent=Upmeter/1.0 (Deckhouse {{ $.Values.global.deckhouseEdition }} {{ $.Values.global.deckhouseVersion }})
          {{- range $probeRef := .Values.upmeter.internal.disabledProbes }}
          - --disable-probe={{ $probeRef }}
          {{- end }}
          {{- if .Values.upmeter.internal.dynamicProbes }}
            {{- range $name := .Values.upmeter.internal.dynamicProbes.ingressControllerNames }}
          - --dynamic-probe-nginx-controller={{ $name }}
            {{- end }}
            {{- range $name := .Values.upmeter.internal.dynamicProbes.cloudEphemeralNodeGroupNames }}
          - --dynamic-probe-nodegroup={{ $name }}
            {{- end }}
          {{- end }}
        env:
          - name: UPMETER_DB_PATH
            value: "/db/downtime.db.sqlite"
          - name: UPMETER_RETENTION_DAYS
            value: "548"
          - name: UPMETER_LISTEN_HOST
            value: 127.0.0.1
          - name: UPMETER_LISTEN_PORT
            value: "8091"
          - name: LOG_LEVEL
            value: "info"
          - name: LOG_TYPE
            value: "json"
          {{- include "helm_lib_envs_for_proxy" . | nindent 10 }}
        volumeMounts:
          - mountPath: /db
            name: data
            readOnly: false
          - mountPath: /tmp
            name: tmp
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: 8443
            scheme: HTTPS
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /ready
            port: 8443
            scheme: HTTPS
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        resources:
          requests:
{{- if $storageClass }}
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
{{- else }}
            {{- include "helm_lib_module_ephemeral_storage_logs_with_extra" 1024 | nindent 12 }}
{{- end }}
{{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "upmeter_resources" . | nindent 12 }}
{{- end }}
      - name: kube-rbac-proxy
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" . | nindent 8 }}
        image: {{ include "helm_lib_module_common_image" (list . "kubeRbacProxy") }}
        args:
        - "--secure-listen-address=$(KUBE_RBAC_PROXY_LISTEN_ADDRESS):8443"
        - "--client-ca-file=/etc/kube-rbac-proxy/ca.crt"
        - "--v=2"
        - "--logtostderr=true"
        - "--stale-cache-interval=1h30m"
        ports:
        - containerPort: 8443
          name: https
        env:
        - name: KUBE_RBAC_PROXY_LISTEN_ADDRESS
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: KUBE_RBAC_PROXY_CONFIG
          value: |
            excludePaths:
            - /healthz
            - /ready
            upstreams:
            - upstream: http://127.0.0.1:8091/
              path: /
              authorization:
                resourceAttributes:
                  namespace: d8-{{ .Chart.Name }}
                  apiGroup: apps
                  apiVersion: v1
                  resource: statefulsets
                  subresource: http
                  name: upmeter
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
{{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "helm_lib_container_kube_rbac_proxy_resources" . | nindent 12 }}
{{- end }}
        volumeMounts:
        - name: kube-rbac-proxy-ca
          mountPath: /etc/kube-rbac-proxy
      volumes:
      - emptyDir: {}
        name: tmp
      - name: kube-rbac-proxy-ca
        configMap:
          defaultMode: 420
          name: kube-rbac-proxy-ca.crt
{{- if not $storageClass }}
      - name: data
        emptyDir: {}
{{- else }}
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: {{ $storageClass }}
      resources:
        requests:
          storage: 1Gi
{{- end }}
